package GUILayer;

import ControlLayer.PersonCtr;

import ModelLayer.Customer;
import ModelLayer.CustomerRecords;
import ModelLayer.RealException;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CustomerUI extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PersonCtr personCtr;
	private CustomerRecords customerRecords;

	private JTextField showAddressField;
	private JTextField showIdField;
	private JTextField showNameField;
	private JTextField showPhoneField;
	private JTextField showGroupField;

	private JTextField editAddressField;
	private JTextField editIdField;
	private JTextField editNameField;
	private JTextField editPhoneField;
	private JTextField editGroupField;

	private JPanel editCustomer;

	private DefaultTableModel tableModel;
	private JTable table;

	private JPanel showCustomer;
	private JPanel showAllCustomers;
	private JTextField showAllIdField;

	private JTextField createAddressField;
	private JTextField createNameField;
	private JTextField createPhoneField;
	private JTextField createGroupField;

	private JPanel createCustomer;

	private JPanel customersContainer;

	public CustomerUI() {
		personCtr = new PersonCtr();
		customerRecords = CustomerRecords.getInstance();
		initialize();
	}

	public void initialize() {

		tableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel.addColumn("ID");
		tableModel.addColumn("Name");
		tableModel.addColumn("Address");

		this.setLayout(new BorderLayout(0, 0));

		JPanel showAllMenu = new JPanel();
		this.add(showAllMenu, BorderLayout.NORTH);
		showAllMenu.setLayout(new BorderLayout(0, 0));
		showAllMenu
				.setBorder(BorderFactory.createTitledBorder("Customer Menu"));

		JPanel showAllMenuLeft = new JPanel();
		showAllMenu.add(showAllMenuLeft, BorderLayout.WEST);

		JButton btnCreateNewCustomer = new JButton("Create new customer");
		btnCreateNewCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) customersContainer.getLayout();
				cl.show(customersContainer, "createCustomer");
			}
		});
		showAllMenuLeft.add(btnCreateNewCustomer);

		JPanel showAllMenuRight = new JPanel();
		showAllMenu.add(showAllMenuRight, BorderLayout.EAST);

		JLabel showAllIdLbl = new JLabel("Id:");
		showAllMenuRight.add(showAllIdLbl);

		showAllIdField = new JTextField();
		showAllMenuRight.add(showAllIdField);
		showAllIdField.setColumns(5);

		JButton showAllBtnSearch = new JButton("Search");
		showAllBtnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showCustomer(Integer.parseInt(showAllIdField.getText()));
				showAllIdField.setText("");
			}
		});
		showAllMenuRight.add(showAllBtnSearch);

		customersContainer = new JPanel();
		this.add(customersContainer);
		customersContainer.setLayout(new CardLayout(0, 0));

		showCustomer = new JPanel();
		customersContainer.add(showCustomer, "showCustomer");
		showCustomer.setLayout(new BorderLayout(0, 0));

		JPanel showCustomerInfoContainer = new JPanel();
		showCustomer.add(showCustomerInfoContainer, BorderLayout.NORTH);
		showCustomerInfoContainer.setLayout(new FlowLayout(FlowLayout.CENTER,
				5, 5));
		showCustomerInfoContainer.setBorder(BorderFactory
				.createTitledBorder("Customer Info"));

		JPanel showCustomerInfo = new JPanel();
		showCustomerInfo.setLayout(new BoxLayout(showCustomerInfo,
				BoxLayout.X_AXIS));
		showCustomerInfoContainer.add(showCustomerInfo);

		JPanel showCustomerLeft = new JPanel();
		showCustomerInfo.add(showCustomerLeft);
		showCustomerLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel showIdLabelPanel = new JPanel();
		showIdLabelPanel.setLayout(new BoxLayout(showIdLabelPanel,
				BoxLayout.X_AXIS));
		showCustomerLeft.add(showIdLabelPanel);

		JLabel showIdLabel = new JLabel("ID");
		showIdLabelPanel.add(showIdLabel);
		showIdLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showNameLabelPanel = new JPanel();
		showNameLabelPanel.setLayout(new BoxLayout(showNameLabelPanel,
				BoxLayout.X_AXIS));
		showCustomerLeft.add(showNameLabelPanel);

		JLabel showNameLabel = new JLabel("Name");
		showNameLabelPanel.add(showNameLabel);
		showNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showAddressLabelPanel = new JPanel();
		showAddressLabelPanel.setLayout(new BoxLayout(showAddressLabelPanel,
				BoxLayout.X_AXIS));
		showCustomerLeft.add(showAddressLabelPanel);

		JLabel showAddressLabel = new JLabel("Address");
		showAddressLabelPanel.add(showAddressLabel);
		showAddressLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showPhoneLabelPanel = new JPanel();
		showPhoneLabelPanel.setLayout(new BoxLayout(showPhoneLabelPanel,
				BoxLayout.X_AXIS));
		showCustomerLeft.add(showPhoneLabelPanel);

		JLabel showPhoneLabel = new JLabel("Phone");
		showPhoneLabelPanel.add(showPhoneLabel);
		showPhoneLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showGroupLabelPanel = new JPanel();
		showGroupLabelPanel.setLayout(new BoxLayout(showGroupLabelPanel,
				BoxLayout.X_AXIS));
		showCustomerLeft.add(showGroupLabelPanel);

		JLabel showGroupLabel = new JLabel("Group");
		showGroupLabelPanel.add(showGroupLabel);
		showGroupLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showCustomerRight = new JPanel();
		showCustomerRight.setLayout(new BoxLayout(showCustomerRight,
				BoxLayout.Y_AXIS));
		showCustomerInfo.add(showCustomerRight);

		JPanel showIdFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showCustomerRight.add(showIdFieldPanel);

		showIdField = new JTextField();
		showIdFieldPanel.add(showIdField);
		showIdField.setHorizontalAlignment(SwingConstants.CENTER);
		showIdField.setEditable(false);
		showIdField.setColumns(5);

		JPanel showNameFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showCustomerRight.add(showNameFieldPanel);

		showNameField = new JTextField();
		showNameFieldPanel.add(showNameField);
		showNameField.setHorizontalAlignment(SwingConstants.CENTER);
		showNameField.setEditable(false);
		showNameField.setColumns(20);

		JPanel showAddressFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showCustomerRight.add(showAddressFieldPanel);

		showAddressField = new JTextField();
		showAddressFieldPanel.add(showAddressField);
		showAddressField.setHorizontalAlignment(SwingConstants.CENTER);
		showAddressField.setEditable(false);
		showAddressField.setColumns(25);

		JPanel showPhoneFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showCustomerRight.add(showPhoneFieldPanel);

		showPhoneField = new JTextField();
		showPhoneFieldPanel.add(showPhoneField);
		showPhoneField.setHorizontalAlignment(SwingConstants.CENTER);
		showPhoneField.setEditable(false);
		showPhoneField.setColumns(15);

		JPanel showGroupFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showCustomerRight.add(showGroupFieldPanel);

		showGroupField = new JTextField();
		showGroupFieldPanel.add(showGroupField);
		showGroupField.setHorizontalAlignment(SwingConstants.CENTER);
		showGroupField.setEditable(false);
		showGroupField.setColumns(5);

		JPanel showBottomMenu = new JPanel();
		showCustomer.add(showBottomMenu, BorderLayout.CENTER);
		showBottomMenu.setLayout(new BorderLayout(0, 0));

		JPanel showBottomMenuRight = new JPanel();
		showBottomMenu.add(showBottomMenuRight, BorderLayout.CENTER);

		JButton showBtnEdit = new JButton("Edit");
		showBtnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editCustomer((int) Integer.parseInt(showIdField.getText()));
			}
		});
		showBottomMenuRight.add(showBtnEdit);

		JButton showBtnDelete = new JButton("Delete");
		showBtnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int reply = JOptionPane.showConfirmDialog(null,
						"Are you sure you want to delete this customer ?",
						"Delete Customer", JOptionPane.YES_NO_OPTION,
						JOptionPane.WARNING_MESSAGE);
				if (reply == JOptionPane.YES_OPTION) {
					boolean success = true;
					try {
						personCtr.deleteCustomer(Integer.parseInt(showIdField
								.getText()));
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane.showMessageDialog(null,
								"The customer was deleted", "Delete Customer",
								JOptionPane.INFORMATION_MESSAGE);
						showAllCustomers();
					}
				}
			}
		});
		showBottomMenuRight.add(showBtnDelete);

		JPanel showBottomMenuLeft = new JPanel();
		showBottomMenu.add(showBottomMenuLeft, BorderLayout.WEST);

		JButton showBtnBack = new JButton("Back");
		showBtnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllCustomers();
			}
		});
		showBottomMenuLeft.add(showBtnBack);

		editCustomer = new JPanel();
		customersContainer.add(editCustomer, "editCustomer");
		editCustomer.setLayout(new BorderLayout(0, 0));

		JPanel editCustomerInfoContainer = new JPanel();
		editCustomer.add(editCustomerInfoContainer, BorderLayout.NORTH);
		editCustomerInfoContainer.setLayout(new FlowLayout(FlowLayout.CENTER,
				5, 5));
		editCustomerInfoContainer.setBorder(BorderFactory
				.createTitledBorder("Edit Customer"));

		JPanel editCustomerInfo = new JPanel();
		editCustomerInfo.setLayout(new BoxLayout(editCustomerInfo,
				BoxLayout.X_AXIS));
		editCustomerInfoContainer.add(editCustomerInfo);

		JPanel editCustomerLeft = new JPanel();
		editCustomerInfo.add(editCustomerLeft);
		editCustomerLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel editIdLabelPanel = new JPanel();
		editIdLabelPanel.setLayout(new BoxLayout(editIdLabelPanel,
				BoxLayout.X_AXIS));
		editCustomerLeft.add(editIdLabelPanel);

		JLabel editIdLabel = new JLabel("ID");
		editIdLabelPanel.add(editIdLabel);
		editIdLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editNameLabelPanel = new JPanel();
		editNameLabelPanel.setLayout(new BoxLayout(editNameLabelPanel,
				BoxLayout.X_AXIS));
		editCustomerLeft.add(editNameLabelPanel);

		JLabel editNameLabel = new JLabel("Name");
		editNameLabelPanel.add(editNameLabel);
		editNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editAddressLabelPanel = new JPanel();
		editAddressLabelPanel.setLayout(new BoxLayout(editAddressLabelPanel,
				BoxLayout.X_AXIS));
		editCustomerLeft.add(editAddressLabelPanel);

		JLabel editAddressLabel = new JLabel("Address");
		editAddressLabelPanel.add(editAddressLabel);
		editAddressLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editPhoneLabelPanel = new JPanel();
		editPhoneLabelPanel.setLayout(new BoxLayout(editPhoneLabelPanel,
				BoxLayout.X_AXIS));
		editCustomerLeft.add(editPhoneLabelPanel);

		JLabel editPhoneLabel = new JLabel("Phone");
		editPhoneLabelPanel.add(editPhoneLabel);
		editPhoneLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editGroupLabelPanel = new JPanel();
		editGroupLabelPanel.setLayout(new BoxLayout(editGroupLabelPanel,
				BoxLayout.X_AXIS));
		editCustomerLeft.add(editGroupLabelPanel);

		JLabel editGroupLabel = new JLabel("Group");
		editGroupLabelPanel.add(editGroupLabel);
		editGroupLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editCustomerRight = new JPanel();
		editCustomerRight.setLayout(new BoxLayout(editCustomerRight,
				BoxLayout.Y_AXIS));
		editCustomerInfo.add(editCustomerRight);

		JPanel editIdFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editCustomerRight.add(editIdFieldPanel);

		editIdField = new JTextField();
		editIdField.setEditable(false);
		editIdFieldPanel.add(editIdField);
		editIdField.setHorizontalAlignment(SwingConstants.CENTER);
		editIdField.setColumns(5);

		JPanel editNameFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		editCustomerRight.add(editNameFieldPanel);

		editNameField = new JTextField();
		editNameFieldPanel.add(editNameField);
		editNameField.setHorizontalAlignment(SwingConstants.CENTER);
		editNameField.setColumns(20);

		JPanel editAddressFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editCustomerRight.add(editAddressFieldPanel);

		editAddressField = new JTextField();
		editAddressFieldPanel.add(editAddressField);
		editAddressField.setHorizontalAlignment(SwingConstants.CENTER);
		editAddressField.setColumns(25);

		JPanel editPhoneFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editCustomerRight.add(editPhoneFieldPanel);

		editPhoneField = new JTextField();
		editPhoneFieldPanel.add(editPhoneField);
		editPhoneField.setHorizontalAlignment(SwingConstants.CENTER);
		editPhoneField.setColumns(15);

		JPanel editGroupFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editCustomerRight.add(editGroupFieldPanel);

		editGroupField = new JTextField();
		editGroupFieldPanel.add(editGroupField);
		editGroupField.setHorizontalAlignment(SwingConstants.CENTER);
		editGroupField.setColumns(5);

		JPanel editButtonsPanel = new JPanel();
		editCustomer.add(editButtonsPanel, BorderLayout.CENTER);
		editButtonsPanel.setLayout(new BorderLayout(0, 0));

		JPanel editCenterButtonsPanel = new JPanel();
		editButtonsPanel.add(editCenterButtonsPanel, BorderLayout.CENTER);

		JButton editBtnEdit = new JButton("Edit");
		editBtnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int reply = JOptionPane.showConfirmDialog(null,
						"Are you sure you want to edit this customer ?",
						"Edit Customer", JOptionPane.YES_NO_OPTION,
						JOptionPane.WARNING_MESSAGE);
				if (reply == JOptionPane.YES_OPTION) {
					boolean success = true;
					try {
						personCtr.updateCustomer(
								Integer.parseInt(editIdField.getText()), 1,
								editNameField.getText());
						personCtr.updateCustomer(
								Integer.parseInt(editIdField.getText()), 2,
								editAddressField.getText());
						personCtr.updateCustomer(
								Integer.parseInt(editIdField.getText()), 3,
								editPhoneField.getText());
						personCtr.updateCustomer(
								Integer.parseInt(editIdField.getText()), 4,
								editGroupField.getText());
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane.showMessageDialog(null,
								"The customer was edited", "Edit Customer",
								JOptionPane.INFORMATION_MESSAGE);
						showCustomer(Integer.parseInt(showIdField.getText()));
					}
				}
			}
		});
		editCenterButtonsPanel.add(editBtnEdit);

		JButton editBtnCancel = new JButton("Cancel");
		editBtnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showCustomer((int) Integer.parseInt(editIdField.getText()));
			}
		});
		editCenterButtonsPanel.add(editBtnCancel);

		createCustomer = new JPanel();
		customersContainer.add(createCustomer, "createCustomer");
		createCustomer.setLayout(new BorderLayout(0, 0));

		JPanel createCustomerInfoContainer = new JPanel();
		createCustomer.add(createCustomerInfoContainer, BorderLayout.NORTH);
		createCustomerInfoContainer.setLayout(new FlowLayout(FlowLayout.CENTER,
				5, 5));
		createCustomerInfoContainer.setBorder(BorderFactory
				.createTitledBorder("Create customer"));

		JPanel createCustomerInfo = new JPanel();
		createCustomerInfo.setLayout(new BoxLayout(createCustomerInfo,
				BoxLayout.X_AXIS));
		createCustomerInfoContainer.add(createCustomerInfo);

		JPanel createCustomerLeft = new JPanel();
		createCustomerInfo.add(createCustomerLeft);
		createCustomerLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel createNameLabelPanel = new JPanel();
		createNameLabelPanel.setLayout(new BoxLayout(createNameLabelPanel,
				BoxLayout.X_AXIS));
		createCustomerLeft.add(createNameLabelPanel);

		JLabel createNameLabel = new JLabel("Name");
		createNameLabelPanel.add(createNameLabel);
		createNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel createAddressLabelPanel = new JPanel();
		createAddressLabelPanel.setLayout(new BoxLayout(
				createAddressLabelPanel, BoxLayout.X_AXIS));
		createCustomerLeft.add(createAddressLabelPanel);

		JLabel createAddressLabel = new JLabel("Address");
		createAddressLabelPanel.add(createAddressLabel);
		createAddressLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel createPhoneLabelPanel = new JPanel();
		createPhoneLabelPanel.setLayout(new BoxLayout(createPhoneLabelPanel,
				BoxLayout.X_AXIS));
		createCustomerLeft.add(createPhoneLabelPanel);

		JLabel createPhoneLabel = new JLabel("Phone");
		createPhoneLabelPanel.add(createPhoneLabel);
		createPhoneLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel createGroupLabelPanel = new JPanel();
		createGroupLabelPanel.setLayout(new BoxLayout(createGroupLabelPanel,
				BoxLayout.X_AXIS));
		createCustomerLeft.add(createGroupLabelPanel);

		JLabel createGroupLabel = new JLabel("Group");
		createGroupLabelPanel.add(createGroupLabel);
		createGroupLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel createCustomerRight = new JPanel();
		createCustomerRight.setLayout(new BoxLayout(createCustomerRight,
				BoxLayout.Y_AXIS));
		createCustomerInfo.add(createCustomerRight);

		JPanel createNameFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		createCustomerRight.add(createNameFieldPanel);

		createNameField = new JTextField();
		createNameFieldPanel.add(createNameField);
		createNameField.setHorizontalAlignment(SwingConstants.CENTER);
		createNameField.setColumns(20);

		JPanel createAddressFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		createCustomerRight.add(createAddressFieldPanel);

		createAddressField = new JTextField();
		createAddressFieldPanel.add(createAddressField);
		createAddressField.setHorizontalAlignment(SwingConstants.CENTER);
		createAddressField.setColumns(25);

		JPanel createPhoneFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		createCustomerRight.add(createPhoneFieldPanel);

		createPhoneField = new JTextField();
		createPhoneFieldPanel.add(createPhoneField);
		createPhoneField.setHorizontalAlignment(SwingConstants.CENTER);
		createPhoneField.setColumns(15);

		JPanel createGroupFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		createCustomerRight.add(createGroupFieldPanel);

		createGroupField = new JTextField();
		createGroupFieldPanel.add(createGroupField);
		createGroupField.setHorizontalAlignment(SwingConstants.CENTER);
		createGroupField.setColumns(5);

		JPanel createBottomMenu = new JPanel();
		createCustomer.add(createBottomMenu, BorderLayout.CENTER);
		createBottomMenu.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton createBtnCreate = new JButton("Create");
		createBottomMenu.add(createBtnCreate);

		JButton createBtnCancel = new JButton("Cancel");
		createBtnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllCustomers();
			}
		});
		createBottomMenu.add(createBtnCancel);

		showAllCustomers = new JPanel();
		customersContainer.add(showAllCustomers, "showAllCustomers");
		showAllCustomers.setLayout(new BorderLayout(0, 0));

		JPanel showAllCustomersPanel = new JPanel();
		showAllCustomersPanel.setBorder(BorderFactory
				.createTitledBorder("All customers"));
		showAllCustomers.add(showAllCustomersPanel, BorderLayout.CENTER);

		table = new JTable(tableModel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

		table.setAutoCreateRowSorter(true);

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int row = table.rowAtPoint(e.getPoint());
				if (e.getClickCount() == 2) {
					showCustomer(Integer.valueOf((Integer) tableModel
							.getValueAt(row, 0)));
				}
			}
		});
		showAllCustomersPanel.setLayout(new BoxLayout(showAllCustomersPanel,
				BoxLayout.X_AXIS));

		JScrollPane scrollPane = new JScrollPane();
		showAllCustomersPanel.add(scrollPane);

		scrollPane.setViewportView(table);
		createBtnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean success = true;
				try {
					personCtr.createCustomer(createNameField.getText(),
							createAddressField.getText(),
							Long.parseLong(createPhoneField.getText()),
							Integer.parseInt(createGroupField.getText()));
				} catch (NumberFormatException e1) {
					success = false;
					JOptionPane.showMessageDialog(null, "Field value invalid",
							"Error", JOptionPane.ERROR_MESSAGE);
				} catch (RealException e1) {
					success = false;
					JOptionPane.showMessageDialog(null, e1.getError(), "Error",
							JOptionPane.ERROR_MESSAGE);
				}
				if (success) {
					JOptionPane.showMessageDialog(null,
							"The customer was created", "Create Customer",
							JOptionPane.INFORMATION_MESSAGE);
					showAllCustomers();
				}
			}
		});
		showAllCustomers();
	}

	protected void showCustomer(int id) {
		boolean success = true;
		Customer customer = null;
		try {
			customer = customerRecords.getCustomer(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			showIdField.setText(Integer.toString(customer.getId()));
			showNameField.setText(customer.getName());
			showAddressField.setText(customer.getAddress());
			showPhoneField.setText(Long.toString(customer.getPhone()));
			showGroupField.setText(Integer
					.toString(customer.getGroup().getId()));
			CardLayout cl = (CardLayout) (customersContainer.getLayout());
			cl.show(customersContainer, "showCustomer");
		}
	}

	protected void editCustomer(int id) {
		Customer customer = null;
		boolean success = true;
		try {
			customer = customerRecords.getCustomer(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			editIdField.setText(Integer.toString(customer.getId()));
			editNameField.setText(customer.getName());
			editAddressField.setText(customer.getAddress());
			editPhoneField.setText(Long.toString(customer.getPhone()));
			editGroupField.setText(Integer
					.toString(customer.getGroup().getId()));
			CardLayout cl = (CardLayout) (customersContainer.getLayout());
			cl.show(customersContainer, "editCustomer");
		}
	}

	protected void showAllCustomers() {
		ArrayList<Customer> customers = customerRecords.getCustomers();
		tableModel.setRowCount(0);
		for (Customer customer : customers) {
			tableModel.addRow(new Object[] { customer.getId(),
					customer.getName(), customer.getAddress() });
		}
		CardLayout cl = (CardLayout) (customersContainer.getLayout());
		cl.show(customersContainer, "showAllCustomers");
	}

}
